package cn.trip.biz.service.impl;

import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripUseLinkUser;
import cn.itrip.beans.pojo.ItripUser;

import cn.itrip.common.RedisUtil;
import cn.itrip.dao.ItripUseLinkUserMapper;
import cn.itrip.dao.ItripUserMapper;
import cn.trip.biz.service.FindUsertoUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.file.attribute.UserDefinedFileAttributeView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service("FindUsertoUserService")
public class FindUsertoUserServiceImpl implements FindUsertoUserService {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ItripUserMapper itripUserMapper;
    @Autowired
    private ItripUseLinkUserMapper itripUseLinkUserMapper;

    @Override
    public List<ItripUseLinkUser> fingUsertoUser2(long id) {
        List<ItripUser> itripUsers = itripUserMapper.findUsertoUser2(id);
        List linkUser = null;
        for (ItripUser w : itripUsers) {
            linkUser = w.getItripUseLinkUsers();
        }
        return linkUser;
    }


    @Override
    public List<ItripUseLinkUser> fingUsertoUser(ItripUser users) {
        long userid = users.getId();
        List<ItripUser> itripUsers = null;
        List<ItripUseLinkUser> list1 = null;
        List<ItripUseLinkUser> newlist1 = null;
        List linkUser = null;
        String user = "linkuser:" + userid;
        ;
        if (this.getredis(String.valueOf(user)) == null) {
            itripUsers = itripUserMapper.findUsertoUser(userid);
            for (ItripUser w : itripUsers) {
                linkUser = w.getItripUseLinkUsers();
            }
            this.setredis(String.valueOf(user), linkUser);
            newlist1 = this.fingUsertoUser2(userid);
        } else {
            newlist1 = (List<ItripUseLinkUser>) this.getredis(String.valueOf(user));
//            newlist1  = new ArrayList<>();
//            for (int i = start; i < (start + size); i++) {
//                newlist1.add(list1.get(i));
//            }
        }
        return newlist1;
    }

    @Override
    public List<ItripUseLinkUser> findUsertosession(String conditions, ItripUser users) {
        List<ItripUseLinkUser> user2 = null;
        Map user3 = new HashMap();
        long id = users.getId();
        ItripUser itripUser = new ItripUser();
        Dto dto = new Dto();
        if (conditions != null && conditions != "") {
            user2 = this.findUsertoUserByconditions(id, conditions);
        } else {
            user2 = this.fingUsertoUser(users);
        }
//        dto.setResult("成功");
//        itripUser.setDto(dto);
//        user3.put("user2", user2);
//        user3.put("itripUser", itripUser);
//        return user3;
//        }
        return user2;

    }

    @Override
    public ItripUser selectUserid(String userCode) {
        return itripUserMapper.selectUserid(userCode);
    }

    @Override
    public void setredis(Object key, Object value) {
        redisUtil.set((String) key, value, 600);
    }

    @Override
    public Object getredis(String key) {
        return redisUtil.get(key);
    }

    @Override
    public int selectCount(long Id) {
        int num = 0;
        if (this.getredis(String.valueOf(Id)) == null) {

            num = itripUserMapper.selectCount(Id);
            this.setredis(String.valueOf(Id), num);
        } else {
            num = (int) this.getredis(String.valueOf(Id));
        }
        return num;
    }

    /**
     * 模糊查询
     *
     * @param id
     * @param
     * @param
     * @param conditions
     * @return
     */
    @Override
    public List<ItripUseLinkUser> findUsertoUserByconditions(long id, String conditions) {
        int ww = 0;
        int num = 0;
        List<ItripUser> itripUsers = null;
        List<ItripUseLinkUser> list1 = null;
        List<ItripUseLinkUser> newlist1 = null;
        List linkUser = null;
        String user = "linkuser:" + id;
        if (this.getredis(String.valueOf(user)) == null) {
            itripUsers = itripUserMapper.findUsertoUser(id);
            for (ItripUser w : itripUsers) {
                linkUser = w.getItripUseLinkUsers();
            }
            this.setredis(String.valueOf(user), linkUser);
            newlist1 = this.findUsertoUserByconditions2(id, conditions);

        } else {
            list1 = (List<ItripUseLinkUser>) this.getredis(String.valueOf(user));
            newlist1 = new ArrayList<>();
            for (ItripUseLinkUser us : list1) {
                num++;
                if (us.getLinkUserName().contains(conditions)) {
                    int nums = num - 1;
                    ww++;
//                    if (ww <= (start + size)) {
//                        for (int i = ww-1; i < ww; i++) {
//                    for(){
                    newlist1.add(list1.get(nums));
//                        }
//                    }
//                    if (ww > (start + size)) {
//                        for (int i = start; i < (start + size); i++) {
//                            newlist1.add(list1.get(i));
//                        }
                }


            }

        }

        return newlist1;
    }

    @Override
    public List<ItripUseLinkUser> findUsertoUserByconditions2(long id, String conditions) {
        List<ItripUser> itripUsers = itripUserMapper.findUsertoUserByconditions(id, conditions);
        List linkUser = null;
        for (ItripUser w : itripUsers) {
            linkUser = w.getItripUseLinkUsers();
        }
        return linkUser;
    }

    /**
     * 根据用户token检查接收的用户id是不是当前token的
     *
     * @param id
     * @param token
     * @return
     */
    @Override
    public boolean idIsOk(Long id, String token) {
        ItripUser itripUser = (ItripUser) redisUtil.get(token);
        if (itripUser.getId().equals(id)) {
            return true;
        }
        return false;
    }

    /**
     * 查询常用旅客
     *
     * @param users
     * @return
     */
    @Override
    public boolean setlinkUser(ItripUseLinkUser users) {
        long id = users.getId();
        int userid = users.getUserId();
        String linkPhone = users.getLinkPhone();
        String regex1 = "^(0\\d{2}-\\d{8}(-\\d{1,4})?)|(0\\d{3}-\\d{7,8}(-\\d{1,4})?)$";
        String linkIdCard = users.getLinkIdCard();
        String regex2 = "^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";
        boolean flag = this.check(linkPhone, regex1);
        boolean flag2 = this.check(linkIdCard, regex2);
        if (!flag && !flag2) {
            if (itripUseLinkUserMapper.updateByPrimaryKeySelective(users) >= 1) {
                if (redisUtil.hasKey("linkuser:" + userid)) {
                    redisUtil.deletePrefix("linkuser:" + userid);
                }
                return true;
            }
            return false;
        } else {
            return false;
        }
    }

    /**
     * @param str   手机号或身份证
     * @param regex 正则表达式
     * @return
     */
    @Override
    public boolean check(String str, String regex) {

        boolean flag = true;
        try {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(str);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

    /**
     * 添加常用旅客
     *
     * @param linkUser
     * @return
     */
    @Override
    public boolean addlinkUser(ItripUseLinkUser linkUser) {
        int userid = linkUser.getUserId();
        String linkPhone = linkUser.getLinkPhone();
        String regex1 = "^(0\\d{2}-\\d{8}(-\\d{1,4})?)|(0\\d{3}-\\d{7,8}(-\\d{1,4})?)$";
        String linkIdCard = linkUser.getLinkIdCard();
        String regex2 = "^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";
        boolean flag = this.check(linkPhone, regex1);
        boolean flag2 = this.check(linkIdCard, regex2);
        if (!flag && !flag2) {
            if (itripUseLinkUserMapper.insertSelective(linkUser) >= 1) {
                if (redisUtil.hasKey("linkuser:" + userid)) {
                    redisUtil.deletePrefix("linkuser:" + userid);
                }
                return true;
            }
            return false;
        } else {
            return false;
        }
    }

    /**
     * 删除常用旅客
     *
     * @param id     主键
     * @param userid 用户id
     *               ridis 的键是  linkuser：+userid
     * @return
     */
    @Override
    public Dto delect(long id, int userid) {
        Dto dto = new Dto();
        ItripUser itripUser = itripUserMapper.booleandelect(userid);
        if (itripUser == null) {
            if (itripUseLinkUserMapper.deleteByPrimaryKey(id) >= 1) {
                if (redisUtil.hasKey("linkuser:" + userid)) {
                    redisUtil.deletePrefix("linkuser:" + userid);
                }
                dto.setSuccess("true");
                dto.setErrorCode("0");
                dto.setMsg("删除成功");
            } else {
                dto.setSuccess("false");

                dto.setMsg("删除失败");
            }
        } else {
            dto.setSuccess("false");
            dto.setMsg("所选的常用联系人中有与某条待支付的订单关联的项，无法删除");
        }
        return dto;
    }
}
